EntityEvents.spawned(event => {
    const entity = event.entity;
    const world = event.level;
    // 检查生成的实体是否为僵尸
    if (entity.type === 'minecraft:zombie') {
        const chance = (Math.random()+Math.random())/2
        // 按比例替换为其他生物
        if (chance < 0.00085) { //概率替换
            const mob = 'mobz:bigboss'
            const newEntity = world.createEntity(mob);
            newEntity.setPos(entity.x, entity.y, entity.z); 
            world.addFreshEntity(newEntity); 
            return
        }
        else if(chance < 0.0085){
            const mob = 'mobz:tank_zombie'
            const newEntity = world.createEntity(mob);
            newEntity.setPos(entity.x, entity.y, entity.z); 
            world.addFreshEntity(newEntity); 
            return
        }
        else if(chance < 0.075){
            const mob = 'mobz:small_zombie'
            const newEntity = world.createEntity(mob);
            newEntity.setPos(entity.x, entity.y, entity.z); 
            world.addFreshEntity(newEntity); 
            return
        }
        else if (chance < 0.145){
            const mob = 'mobz:fast_zombie'
            const newEntity = world.createEntity(mob);
            newEntity.setPos(entity.x, entity.y, entity.z); 
            world.addFreshEntity(newEntity); 
            return
        }
        else if (chance < 0.2){
            const mob = 'mobz:ender_zombie'
            const newEntity = world.createEntity(mob);
            newEntity.setPos(entity.x, entity.y, entity.z); 
            world.addFreshEntity(newEntity); 
            return
        }  
        else if(chance >0.85){
            event.cancel()
        }  
    }
    if (entity.type === 'minecraft:creeper') {
        const chance = (Math.random()+Math.random())/2
        if (chance < 0.0333) {
            const mob = 'mobz:frost_creeper'
            const newEntity = world.createEntity(mob);
            newEntity.setPos(entity.x, entity.y, entity.z); 
            world.addFreshEntity(newEntity); 
            return
        }
        else if(chance < 0.0666){
            const mob = 'mobz:cookie_creeper'
            const newEntity = world.createEntity(mob);
            newEntity.setPos(entity.x, entity.y, entity.z); 
            world.addFreshEntity(newEntity); 
            return
        }
        else if(chance >0.85){
            event.cancel()
        } 
    }
    if (entity.type === 'minecraft:skeleton') {
        const chance = (Math.random()+Math.random())/2
        if (chance < 0.0085) {
            const mob = 'mobz:boss_skeleton'
            const newEntity = world.createEntity(mob);
            newEntity.setPos(entity.x, entity.y, entity.z); 
            world.addFreshEntity(newEntity); 
            return
        }
        else if(chance < 0.10){
            const mob = 'mobz:lost_skeleton'
            const newEntity = world.createEntity(mob);
            newEntity.setPos(entity.x, entity.y, entity.z); 
            world.addFreshEntity(newEntity); 
            return
        }
        else if(chance < 0.20){
            const mob = 'mobz:overgrown_skeleton'
            const newEntity = world.createEntity(mob);
            newEntity.setPos(entity.x, entity.y, entity.z); 
            world.addFreshEntity(newEntity); 
            return
        }
        else if(chance >0.85){
            event.cancel()
        } 
    }
    if (entity.type === 'minecraft:spider') {
        const chance = (Math.random()+Math.random())/2
        if (chance < 0.05) { 
            const mob = 'mobz:tiny_spider'
            const newEntity = world.createEntity(mob);
            newEntity.setPos(entity.x, entity.y, entity.z); 
            world.addFreshEntity(newEntity); 
            return
        }
        if (chance < 0.10) { 
            const mob = 'mobz:small_spider'
            const newEntity = world.createEntity(mob);
            newEntity.setPos(entity.x, entity.y, entity.z); 
            world.addFreshEntity(newEntity); 
            return
        }
        if (chance < 0.15) { 
            const mob = 'mobz:purple_spider'
            const newEntity = world.createEntity(mob);
            newEntity.setPos(entity.x, entity.y, entity.z); 
            world.addFreshEntity(newEntity); 
            return
        }
        if (chance < 0.20) { 
            const mob = 'mobz:blue_spider'
            const newEntity = world.createEntity(mob);
            newEntity.setPos(entity.x, entity.y, entity.z); 
            world.addFreshEntity(newEntity); 
            return 
        }
        else if(chance >0.85){
            event.cancel()
        }
    }
});
// 冷却时间（5 分钟，单位为 tick）
const COOLDOWN_TICKS = 5 * 60 * 20; // 5 分钟 = 6000 ticks
const summons = new Map();
summons.set('cool',0)
PlayerEvents.tick(event => {
    const {player,server} = event // 获取玩家对象
    const cool = summons.get('cool')
    if (cool > 0) {
        summons.set('cool',cool--) // 减少冷却时间
        return;} // 冷却时间内不执行检测
    const offhand = player.offHandItem; // 获取玩家背包
    const y = player.y;const x = player.x;const z = player.z
        if (offhand.hasTag('forge:gems/emerald') && offhand.getCount() >=10) { // 检查是否为绿宝石
            // 随机决定是否触发事件
            if (Math.random() < 0.0033) {
                player.tell('查尔斯国王出现了！'); // 发送消息给玩家
                server.runCommandSilent(`summon mobz:charles ${Math.floor(x)} ${Math.floor(y+4)} ${Math.floor(z)}`);
                server.runCommandSilent(`summon mobz:william ${Math.floor(x)} ${Math.floor(y+4)} ${Math.floor(z)}`);
                server.runCommandSilent(`summon mobz:andriu ${Math.floor(x)} ${Math.floor(y+4)} ${Math.floor(z)}`);
                server.runCommandSilent(`execute at @e[limit=1,type=mobz:charles] run effect give @e[distance=..3,type=!player] minecraft:slow_falling 3 0`)
                summons.set('cool',COOLDOWN_TICKS) // 设置冷却时间
            }
        }


});